package com.kaiyidiannao.model.req.model;

import android.util.Log;
import com.kaiyidiannao.data.DataCenter;
import com.kaiyidiannao.https.HttpUtils;
import com.kaiyidiannao.model.Result;
import com.kaiyidiannao.model.bean.TourBean;
import com.kaiyidiannao.model.req.base.BaseCommand;
import com.taplinker.core.util.json.JsonUtil;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 *  凑队去-微记 -关键字搜索异步请求
 *  @author david
 */
public class GetTourSamallNoteSearchLIst extends BaseCommand {

    private int page =  DataCenter.LISTVIEW_DEFAULT_PAGE; //当前页码
    private String TAG = "GetTourSamallNoteSearchLIst";

    //搜索关键字
    private String title;
    private int catid;

    public GetTourSamallNoteSearchLIst(String title, int page,  int catid) {
        this.title = title;
        this.page = page;
        this.catid = catid;
    }

    @Override
    public String getRequestUrl() {
        return DataCenter.GET_TOUR_SMALL_NOTE_SEARCH;
    }

    @Override
    public HashMap<String, String> getRequestParamters() {
        HashMap<String, String> paramters = new HashMap<String, String>();
        paramters.put("title", title);
        paramters.put("page",page + "");
        paramters.put("catid",catid + "");
        return paramters;
    }

    @Override
    public Result go(String url, HashMap<String, String> paramters) { // 联网时后台执行
        // 根据请求参数返回请求数据
        return HttpUtils.getByHttpClient(url, paramters);
    }


    @Override
    public void yes(Result result) { //后台执行完毕后执行
        super.yes(result);
        Log.d(DataCenter.LOG_INFO, "GetTourSamallNoteSearchLIst.yes()");

        JSONArray jsonArray = null;
        try {
            Log.d(TAG, result.object.toString());

            JSONObject jsonObject = new JSONObject(result.object.toString());
            jsonArray = jsonObject.optJSONArray("data");
            
            ArrayList<TourBean> targetList = DataCenter.tourList;
            if (page == DataCenter.LISTVIEW_DEFAULT_PAGE) {
                targetList.clear();
            }

            for(int i = 0; i < jsonArray.length(); ++i){ // i从0开始，屏蔽提示信息
                JSONObject o = jsonArray.getJSONObject(i);
                TourBean bean = JsonUtil.jsonToObject(o.toString(), TourBean.class);
                targetList.add(bean);
            }
            Log.d(TAG, targetList.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }

        DataCenter.getInstance().callBacks(DataCenter.flag.GET_DAY_DAY_COUPON_LIST_SUCCESS);

        if (jsonArray != null && jsonArray.length() < 1) {
            DataCenter.getInstance().callBacks(DataCenter.flag.GET_DAY_DAY_COUPON_LIST_NO_MORE_DATA);
        }
    }

    @Override
    public void no(Result result) {
        super.no(result);
        Log.d(DataCenter.LOG_ERROR, "GetTourSamallNoteSearchLIst.no()");
        DataCenter.tourList.clear();
        DataCenter.getInstance().callBacks(DataCenter.flag.COMMON_REQUEST_FAILED);
    }
}
